'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const {
		user_id,
		article_id
	} = event
	
	const user = await db.collection('user').doc(user_id).get()
	const thumbs_up = user.data[0].thumbs_up_article_ids
	let thumbs_up_ids = null
	let thumbs_up_counts = null
	if(thumbs_up.includes(article_id)){
		thumbs_up_ids = dbCmd.pull(article_id)
		thumbs_up_counts = dbCmd.inc(-1) //原子操作 减少请求
	}else{
		thumbs_up_ids = dbCmd.addToSet(article_id)
		thumbs_up_counts = dbCmd.inc(1)
	}
	
	await db.collection('user').doc(user_id).update({	
		thumbs_up_article_ids:thumbs_up_ids
	})
	
	await db.collection('article').doc(article_id).update({
		thumbs_up_count : thumbs_up_counts
	})
	//返回数据给客户端
	return {
		code:200,
		msg:'update thumbsup'
	}
};
